clear;
random = 2;

switch random
    case 0
        m=100;
        n=m;
    case 2 %file        
        wd=cd;
        cd ..;
        conf=load('config.txt')
        m=conf(1);
        n=m;
        cd(wd);
    otherwise
        m=3;
        n=3;
end


%singular - LU (if det==0)
for i = 1:m,
    for j = 1:n,                
        SIN(i,j) = rand;
    end
    SIN(i,fix(m/2)+1)=0;    
end
wd=cd;
cd ..;
man=sprintf('%s%d%s','maticeLU',size(SIN,1),'.csv');
DLMWRITE(man,SIN,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);


%%sym. pos def. CHOL(S P D)
for i = 1:m,
    for j = 1:n,        
        if i <= j
            a=rand;
            SPD(i,j) = a;   
            SPD(j,i) = a;
        end
    end
    SPD(i,i)=i+1;
end
wd=cd;
cd ..;
man=sprintf('%s%d%s','maticeCHOL',size(SPD,1),'.csv');
DLMWRITE(man,SPD,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
A=randn(m,n);
timeRand=toc;
sprintf('Cas generovani nahodne matice je :%5.3fs',timeRand)
time(1)=timeRand*1000;

wd=cd;
cd ..;
man=sprintf('%s%d%s','maticeA',size(A,1),'.csv');
DLMWRITE(man,A,'delimiter',';','newline','pc','precision','%.18f');

%%generovani matice B
B=randn(m,n);

man=sprintf('%s%d%s','maticeB',size(B,1),'.csv');
DLMWRITE(man,B,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);
    
tic;
X=eye(m,n);
timeEye=toc;
sprintf('Cas 1 na diagonale je :%5.3fs',timeEye)
time(2)=timeEye*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','eye_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=A;
timeCopy=toc;
sprintf('Cas kopirovani :%5.3fs',timeCopy)
time(3)=timeCopy*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','copy_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
exdata(1,1) = {A};
timeArray=toc;
sprintf('Cas prevedeni na pole :%5.3f',timeArray);
time(4)=timeArray*1000;
clear exdata;

tic;
aget=A(2,2);
timeGet=toc;
sprintf('Cas ziskani prvku v matici :%5.3fs',timeGet)

tic;
X=A(1:fix(m/2),1:fix(n/2));
timeGetMat=toc;
sprintf('Cas ziskani casti matice :%5.3fs',timeGetMat)
time(5)=timeGetMat*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','part_m',size(A,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

aset=5;
tic;
X(2,2)=aset;
timeSet=toc;
sprintf('Cas nastaveni prvku v matici :%5.3fs',timeSet)

tic;
X=A';
timeTran=toc;
sprintf('Cas transponovani matice :%5.3fs',timeTran)
time(6)=timeTran*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','tra_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
anorm1=norm(A,1); %norm1
timeNorm1=toc;
sprintf('Cas norm1 matice :%5.3fs (Maxsum radku)',timeNorm1)
time(7)=timeNorm1*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','norm1_m',size(X,1),'.csv');
DLMWRITE(man,anorm1,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);


%largest singular value of X, max(svd(X))
tic;
anorm2=norm(A,2);
timeNorm2=toc;
sprintf('Cas norm2 matice :%5.3fs',timeNorm2)
time(8)=timeNorm2*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','norm2_m',size(X,1),'.csv');
DLMWRITE(man,anorm2,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);


tic;
anormInf=norm(A,inf);
timeInf=toc;
sprintf('Cas normInfT matice :%5.3fs (Maxsum slopce)',timeInf)
time(9)=timeInf*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','normi_m',size(X,1),'.csv');
DLMWRITE(man,anormInf,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
anormFro=norm(A,'fro');
timeNormFrob=toc;
sprintf('Cas Frobenius norm, sqrt(sum(diag(X"X))) :%5.3fs',timeNormFrob)
time(10)=timeNormFrob*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','normf_m',size(X,1),'.csv');
DLMWRITE(man,anormFro,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=-A;
timeUminus=toc;
sprintf('Cas unary minus :%5.3fs',timeUminus)
time(11)=timeUminus*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','uminus_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=A+B;
timePlus=toc;
sprintf('Cas scitani matic :%5.3fs',timePlus)
time(12)=timePlus*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','plus_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=A-B;
timeMinus=toc;
sprintf('Cas odecitani matic :%5.3fs',timeMinus)
time(13)=timeMinus*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','minus_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=times(A,B);
timeTimes=toc;
sprintf('Cas nasobeni matic poslozkach :%5.3fs',timeTimes)
time(14)=timeTimes*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','times_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic; % arrayRightDivide
X=A./B;
timeARD=toc;
sprintf('Cas deleni matic poslozkach (Right) :%5.3fs',timeARD)
time(15)=timeARD*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','rdivide_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic; % arrayLeftDivide
X=B./A;
timeALD=toc;
sprintf('Cas deleni matic poslozkach (Left) :%5.3fs',timeALD)
time(16)=timeALD*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','ldivide_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

atimes=4;
tic; % arrayLeftDivide
X=A*atimes;
timeAT=toc;
sprintf('Cas nasobeni matice prvkem :%5.3fs',timeAT)
time(17)=timeAT*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','multa_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=A*B;
timeMatrixTimes=toc;
sprintf('Cas nasobeni matic :%5.3fs',timeMatrixTimes)
time(18)=timeMatrixTimes*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','mult_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
X=inv(A);
timeInv=toc;
sprintf('Cas vypoctu inverzni matice :%5.3fs',timeInv)
time(19)=timeInv*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','inv_m',size(X,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

tic;
arank=rank(A);
timeRank=toc;
sprintf('Cas vypoctu rank-u matice :%5.3fs',timeRank)
time(20)=timeRank*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','rank_m',size(A,1),'.csv');
DLMWRITE(man,arank,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);
tic;
X=det(A);
timeDet=toc;
sprintf('Cas vypoctu determinantu matice :%5.3fs',timeDet)
time(21)=timeDet*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','det_m',size(A,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);


tic;
[L,U,P]=lu(SIN);
timeLU=toc;
sprintf('Cas vypoctu LU rozkladu :%5.3fs',timeLU)
time(22)=timeLU*1000;
%X=L*U;

wd=cd;
cd ..;
man=sprintf('%s%d%s','lu_l_m',size(L,1),'.csv');
DLMWRITE(man,L,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','lu_u_m',size(U,1),'.csv');
DLMWRITE(man,U,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','lu_p_m',size(P,1),'.csv');
DLMWRITE(man,P,'delimiter',';','newline','pc','precision','%.2f');
cd(wd);
al=L;
au=U;
ap=P;
clear L;
clear U;

tic;
[Q,R]=qr(A);
timeQR=toc;
sprintf('Cas vypoctu QR rozkladu :%5.3fs',timeQR)
time(23)=timeQR*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','qr_q_m',size(Q,1),'.csv');
DLMWRITE(man,Q,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','qr_r_m',size(R,1),'.csv');
DLMWRITE(man,R,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

%X=Q*R;
clear Q;
clear R;

tic;
X=chol(SPD);
timeCHol=toc;
sprintf('Cas vypoctu Cholesky factorization :%5.3fs',timeCHol)
%Y=X'*X;
time(24)=timeCHol*1000;

wd=cd;
cd ..;
man=sprintf('%s%d%s','chol_m',size(SPD,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

% A = U*S*V'
%Singular value decomposition
tic;
[U,S,V]=svd(A);
timeSVD=toc;
sprintf('Cas vypoctu Singular value decomposition :%5.3fs',timeSVD)
%X = U*S*V';
time(25)=timeSVD*1000;
wd=cd;
cd ..;
man=sprintf('%s%d%s','svd_u_m',size(A,1),'.csv');
DLMWRITE(man,U,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','svd_s_m',size(A,1),'.csv');
DLMWRITE(man,S,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','svd_v_m',size(A,1),'.csv');
DLMWRITE(man,V,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

clear U;
clear S;
clear V;

% Eigenvalues and eigenvectors of a real matrix
% D vl cisla
tic;
[V,D]=eig(A);
timeEig=toc;
sprintf('Cas vypoctu Eigenvalues and eigenvectors of a real matrix :%5.3fs',timeEig)
%X= V*D*V';
time(26)=timeEig*1000;

X=imag(diag(D)); %vektor
Y=real(diag(D)); %vektor 
Z=real(V);
W=imag(V);


wd=cd;
cd ..;
man=sprintf('%s%d%s','eig_rm_m',size(A,1),'.csv');
DLMWRITE(man,Z,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','eig_im_m',size(A,1),'.csv');
DLMWRITE(man,W,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','eig_rv_m',size(A,1),'.csv');
DLMWRITE(man,Y,'delimiter',';','newline','pc','precision','%.18f');
man=sprintf('%s%d%s','eig_iv_m',size(A,1),'.csv');
DLMWRITE(man,X,'delimiter',';','newline','pc','precision','%.18f');
cd(wd);

clear V;
clear D;
clear X;
clear Y;
clear Z;
clear W;

cd ..;
man=sprintf('%s%d%s','time_m',size(A,1),'.csv');
DLMWRITE(man,time','delimiter',';','newline','pc','precision','%.3f');
cd(wd);